1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import {
Button,
Form,
FormButtons,
FormField,
FormSubmitButton,
IconLabel,
Row,
TextField,
} from '@umami/react-zen';
import { useMessages, useTeam, useUpdateQuery } from '@/components/hooks';
import { RefreshCw } from '@/components/icons';
import { getRandomChars } from '@/lib/generate';
const generateId = () => `team_${getRandomChars(16)}`;
export function TeamEditForm({
teamId,
allowEdit,
showAccessCode,
onSave,
}: {
teamId: string;
allowEdit?: boolean;
showAccessCode?: boolean;
onSave?: () => void;
}) {
const team = useTeam();
const { formatMessage, labels, messages, getErrorMessage } = useMessages();
const { mutateAsync, error, isPending, touch, toast } = useUpdateQuery(`/teams/${teamId}`);
const handleSubmit = async (data: any) => {
await mutateAsync(data, {
onSuccess: async () => {
toast(formatMessage(messages.saved));
touch('teams');
touch(`teams:${teamId}`);
onSave?.();
},
});
};
return (
<Form onSubmit={handleSubmit} error={getErrorMessage(error)} defaultValues={{ ...team }}>
{({ setValue }) => {
return (
<>
<FormField name="id" label={formatMessage(labels.teamId)}>
<TextField isReadOnly allowCopy />
</FormField>
<FormField
name="name"
label={formatMessage(labels.name)}
rules={{ required: formatMessage(labels.required) }}
>
<TextField isReadOnly={!allowEdit} />
</FormField>
{showAccessCode && (
<Row alignItems="flex-end" gap>
<FormField
name="accessCode"
label={formatMessage(labels.accessCode)}
style={{ flex: 1 }}
>
<TextField isReadOnly allowCopy />
</FormField>
{allowEdit && (
<Button
onPress={() => setValue('accessCode', generateId(), { shouldDirty: true })}
>
<IconLabel icon={<RefreshCw />} label={formatMessage(labels.regenerate)} />
</Button>
)}
</Row>
)}
{allowEdit && (
<FormButtons justifyContent="flex-end">
<FormSubmitButton variant="primary" isPending={isPending}>
{formatMessage(labels.save)}
</FormSubmitButton>
</FormButtons>
)}
</>
);
}}
</Form>
);
}
|